package 链表.环形链表;



/**
 * @Program:IntelliJ IDEA
 * @description:
 * @author:香蕉你个啵娜娜 l.h-p@qq.com
 * @create:2023-01-06 11:10
 **/

public class Solution {
    public boolean hasCycle(ListNode head){
        if (head == null){
            return false;
        }
        // 快慢两个指针
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null){
            // 慢指针每次走一步
            slow = slow.next;
            // 快指针每次走两步
            fast = fast.next.next;
            // 如果相遇，说明有环，直接返回true
            if (slow == fast){
                return true;
            }
        }
        // 否则就是没环
        return false;
    }
}
